package com.yl.users.common.util

import com.yl.common.pojo.vo.TreeSelect
import com.yl.users.entity.CompanyDept
import com.yl.users.entity.CompanyPermissionsView
import com.yl.users.pojo.dto.CompanyPermissionsDto
import java.util.function.Consumer

/**
 * 权限列表转树形选择框
 */
fun permListToTreeSelect1(list: MutableList<CompanyPermissionsDto>, pId: Long?): List<TreeSelect> {
	val tree: MutableList<TreeSelect> = ArrayList()
	val it: MutableIterator<CompanyPermissionsDto> = list.iterator()
	while (it.hasNext()) {
		val m: CompanyPermissionsDto = it.next()
		if (m.parentId!! == pId
		) {
			tree.add(TreeSelect(id=m.permissionsId!!,name=m.permissionsName!!))
			// 已添加的元素删除掉
			it.remove()
		}
	}
	// 寻找子元素
	tree.forEach(Consumer { n: TreeSelect ->
		n.children = permListToTreeSelect1(list, n.id)

	})
	return tree
}

/**
 * 权限列表转树形明细
 */
fun permListToTree(list: MutableList<CompanyPermissionsDto>, pId: Long?): List<CompanyPermissionsDto> {
	val tree: MutableList<CompanyPermissionsDto> = ArrayList()
	val it: MutableIterator<CompanyPermissionsDto> = list.iterator()
	while (it.hasNext()) {
		val m: CompanyPermissionsDto = it.next()
		if (m.parentId!! == pId
		) {
			tree.add(m)
			// 已添加的元素删除掉
			it.remove()
		}
	}
	// 寻找子元素
	tree.forEach(Consumer { n: CompanyPermissionsDto ->
		n.children = permListToTree(list, n.permissionsId)

	})
	return tree
}


fun permissionsViewListToTree(list: MutableList<CompanyPermissionsView>, pId: Long?): List<CompanyPermissionsView> {
	val tree: MutableList<CompanyPermissionsView> = ArrayList()
	val it: MutableIterator<CompanyPermissionsView> = list.iterator()
	while (it.hasNext()) {
		val m: CompanyPermissionsView = it.next()
		if (m.parentId!!.equals(pId)) {
			tree.add(m)
			// 已添加的元素删除掉
			it.remove()
		}
	}
	// 寻找子元素
	tree.forEach(Consumer { n: CompanyPermissionsView ->
		n.children = permissionsViewListToTree(list, n.id)

	})
	return tree
}


fun permissionsViewListToTreeSelect(list: MutableList<CompanyPermissionsView>, pId: Long?): List<TreeSelect> {
	val tree: MutableList<TreeSelect> = ArrayList()
	val it: MutableIterator<CompanyPermissionsView> = list.iterator()
	while (it.hasNext()) {
		val m: CompanyPermissionsView = it.next()
		if (m.parentId!!.equals(pId)) {
			tree.add(TreeSelect(id=m.id,name=m.permissionsName))
			// 已添加的元素删除掉
			it.remove()
		}
	}
	// 寻找子元素
	tree.forEach(Consumer { n: TreeSelect ->
		n.children = permissionsViewListToTreeSelect(list, n.id)

	})
	return tree
}

fun deptListToTree(list: MutableList<CompanyDept>, pId: Long?): MutableList<CompanyDept> {
	val tree: MutableList<CompanyDept> = ArrayList()
	val it: MutableIterator<CompanyDept> = list.iterator()
	while (it.hasNext()) {
		val m: CompanyDept = it.next()
		if (m.superDeptId == pId) {
			tree.add(m)
			// 已添加的元素删除掉
			it.remove()
		}
	}
	// 寻找子元素
	tree.forEach(Consumer { n: CompanyDept ->
		n.children = deptListToTree(list, n.id)

	})
	return tree
}





